package tech.daoxuan.security.core.validate.code;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.daoxuan.security.core.properties.SecurityProperties;
import tech.daoxuan.security.core.validate.code.sms.DefaultSmsCodeSender;
import tech.daoxuan.security.core.validate.code.sms.SmsCodeSender;

/**
 * @ClassName ValidateCodeBeanConfig
 * @Description TODO
 * @Author 10983
 * @Date 2019/6/22 18:08
 * @Version 1.0
 */
@Configuration
public class ValidateCodeBeanConfig {
        Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private SecurityProperties securityProperties;


    /**
     * 这样写 ，实际在 ImageCodeGenerator 类上 加@commpoent一样
     * 但是这样写，可以更自由的配置
     *  当容器 中没有 imageCodeGenerator 的bean时，才会创建，这样。第三方 才会 可配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageCodeGenerator")
    public ValidateCodeGenerator imageCodeGenerator(){
        ImageCodeGenerator imageCodeGenerator = new ImageCodeGenerator();
        imageCodeGenerator.setSecurityProperties(securityProperties);
        logger.info("插件默认的 imageCodeGenerator 生成器 已经使用");
        return imageCodeGenerator;
    }
    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender smsCodeGenerator(){
        return new DefaultSmsCodeSender();
    }
}
